package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.OrderDelDto;
import com.itheima.dto.OrderListDto;
import com.itheima.mapper.OmsOrderMapper;
import com.itheima.mapper.OrderHistoryMapper;
import com.itheima.pojo.OmsOrder;
import com.itheima.pojo.OmsOrderOperateHistory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class OmsOrderImpl implements OmsOrderApi {
    @Autowired
    private OmsOrderMapper omsOrderMapper;
    @Autowired
    private OrderHistoryMapper orderHistoryMapper;

    /**
     * 逻辑批量删除
     * 将状态改为1
     *
     * @param ids
     */
    @Override
    public void deleteLogic5(Integer[] ids) {
        OmsOrder omsOrder=new OmsOrder();
        ////删除状态：0->未删除；1->已删除
        omsOrder.setDeleteStatus(1);
        for (Integer id : ids) {
            omsOrder.setId(id.longValue());
            omsOrderMapper.updateById(omsOrder);
        }
    }


    /**
     * 分页模糊查询
     *
     * @param orderListDto
     * @return
     */
    @Override
    public IPage<OmsOrder> listPage(OrderListDto orderListDto) {
        //分页
        IPage<OmsOrder> page=new Page<>(orderListDto.getPageNum(),orderListDto.getPageSize());
        LambdaQueryWrapper<OmsOrder> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(orderListDto.getOrderSn()),OmsOrder::getOrderSn,orderListDto.getOrderSn())
                .le(!ObjectUtils.isEmpty(orderListDto.getCreateTime()),OmsOrder::getCreateTime,orderListDto.getCreateTime())
                .eq(!ObjectUtils.isEmpty(orderListDto.getOrderType()),OmsOrder::getOrderType,orderListDto.getOrderType())
                .like(!ObjectUtils.isEmpty(orderListDto.getReceiverKeyword()),OmsOrder::getReceiverName,orderListDto.getReceiverKeyword())
                .eq(!ObjectUtils.isEmpty(orderListDto.getSourceType()),OmsOrder::getSourceType,orderListDto.getSourceType())
                .eq(!ObjectUtils.isEmpty(orderListDto.getStatus()),OmsOrder::getStatus,orderListDto.getStatus())
                .eq(OmsOrder::getDeleteStatus,0)
                .orderByDesc(OmsOrder::getCreateTime)
                .or().like(!ObjectUtils.isEmpty(orderListDto.getReceiverKeyword()),OmsOrder::getReceiverPhone,orderListDto.getReceiverKeyword());

        omsOrderMapper.selectPage(page,wrapper);

        return page;
    }


    /**
     * 待付款 已关闭  已完成 都可以关闭
     *
     * @param ids
     * @param note
     */
    @Override
    public void close(Integer[] ids, String note,String username) {
        OmsOrder omsOrder=new OmsOrder();
        omsOrder.setNote(note);
        for (Integer id : ids) {
            omsOrder.setId(id.longValue());
            omsOrderMapper.updateById(omsOrder);
            //保存操作记录
            //将操作记录保存到操作记录表中
            OmsOrderOperateHistory omsOrderOperateHistory=new OmsOrderOperateHistory();
            omsOrderOperateHistory.setOrderId(id.longValue());
            omsOrderOperateHistory.setOperateMan("后台管理员"+username);
            omsOrderOperateHistory.setNote(note);
            //查询出当前订单的状态
            OmsOrder byId = findById(id);
            omsOrderOperateHistory.setOrderStatus(byId.getStatus());
            omsOrderOperateHistory.setCreateTime(LocalDateTime.now());
            orderHistoryMapper.insert(omsOrderOperateHistory);


        }

    }

    /**
     * 根据id 查询
     *
     * @param id
     * @return
     */
    @Override
    public OmsOrder findById(Integer id) {
        return omsOrderMapper.selectById(id);
    }

    /**
     * 批量发货
     *
     * @param deliveryParamList
     */
    @Override
    public void updateDel(List<OrderDelDto> deliveryParamList) {
        for (OrderDelDto orderDelDto : deliveryParamList) {
            OmsOrder omsOrder=new OmsOrder();
            omsOrder.setId(orderDelDto.getOrderId().longValue());
            omsOrder.setDeliveryCompany(orderDelDto.getDeliveryCompany());
            omsOrder.setDeliverySn(orderDelDto.getDeliverySn());
            omsOrder.setStatus(2);
            omsOrderMapper.updateById(omsOrder);
        }
    }

    @Override
    public void updateById(OmsOrder omsOrder) {
        omsOrderMapper.updateById(omsOrder);
    }
}
